﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FHost1
{
    public partial class FormMyAccount : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Request.Cookies.AllKeys.Contains("fhostuser"))
            {
                Response.Redirect("Default.aspx?login=0");
            }

            if (!IsPostBack)
            {
                GetUserInfo();
            }
        }

        private void GetUserInfo()
        {
            int userId = Convert.ToInt32(Request.Cookies["fhostuser"].Value.Split('~')[0]); 
            using (FHostDataContext dataContext = new FHostDataContext())
            {
                var result = from obj in dataContext.UserInfos
                             where obj.UserID == userId
                             select new { obj.FullName, obj.EmailID };
                foreach (var item in result)
                {
                    txtName.Text = item.FullName;
                    txtEmailID.Text = item.EmailID;
                }
            }
        }

        protected void btnUpdateInfo_Click(object sender, EventArgs e)
        {
            try
            {
                int userId = Convert.ToInt32(Request.Cookies["fhostuser"].Value.Split('~')[0]);
                using (FHostDataContext dataContext = new FHostDataContext())
                {
                    dataContext.UpdateProfileInfo(userId, txtName.Text, txtEmailID.Text);
                }

                GetUserInfo();

                ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Updated successfully!!');", true);
            }
            catch
            {
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Error updating profile info');", true);
            }
        }

        protected void btnUpdatePass_Click(object sender, EventArgs e)
        {
            try
            {
                int userId = Convert.ToInt32(Request.Cookies["fhostuser"].Value.Split('~')[0]);
                using (FHostDataContext dataContext = new FHostDataContext())
                {
                    dataContext.UpdatePassword(userId, txtPassword.Text);
                }

                GetUserInfo();
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Updated successfully!!');", true);
            }
            catch
            {
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Error updating password!!');", true);
            }
        }
    }
}